package com.tent.live;

import android.content.Context;

import com.tencent.rtmp.TXLivePushConfig;
import com.tencent.rtmp.TXLivePusher;
import com.tencent.rtmp.ui.TXCloudVideoView;

/**
 * 推流封装。
 */
public final class LivePushManager {
    private static LivePushManager livePushManager;
    private final Context context;
    private TXCloudVideoView mVideoView;
    private TXLivePusher txLivePusher;
    private String pushUrl = null;
    private TXLivePushConfig txLivePushConfig;

    public LivePushManager(Context context) {
        this.context = context;
    }

    public static void init(Context context) {
        if (livePushManager == null) {
            synchronized (LivePushManager.class) {
                if (livePushManager == null) {
                    livePushManager = new LivePushManager(context);

                }
            }
        }
    }
    //-----------链式设计对象-----------

    /**
     * 设置音频推流。
     */
    public void startAudioPush(String pushUrl) {
        if (txLivePushConfig == null || txLivePusher == null) {
            throw new RuntimeException("txLivePushConfig|txLivePusher must not null");
        }
        this.txLivePushConfig.enablePureAudioPush(true); //为启动纯音频推流，默认false；
        this.txLivePusher.setConfig(txLivePushConfig);
        this.txLivePusher.startPusher(pushUrl);

    }


    /**
     * 启动推流
     */
    public void startCameraPreview() {
        if (txLivePusher == null || mVideoView == null) {
            throw new RuntimeException("txLivePusher|mVideoView must not null");
        }
        this.txLivePusher.startCameraPreview(mVideoView);
    }


    /**
     * 设置推流链接。
     *
     * @param pushUrl
     * @return
     */
    public LivePushManager setPushUrl(String pushUrl) {
        this.pushUrl = pushUrl;
        return this;
    }

    /**
     * 创建关联对象
     *
     * @param txLivePushConfig
     * @return
     */
    public LivePushManager setPushConfig(TXLivePushConfig txLivePushConfig) {
        this.txLivePushConfig = txLivePushConfig;
        return this;
    }

    /**
     * 配置 config 关联推流对象。 最后执行 不然抛出异常
     *
     * @return
     */
    public LivePushManager setConfig() {
        if (txLivePusher == null || txLivePushConfig == null) {
            throw new RuntimeException("please init()--> setTxLivePusher()--->setPushConfig()");
        }
        this.txLivePusher.setConfig(txLivePushConfig);
        return this;
    }


    /**
     * 链式设置进去
     *
     * @param txLivePusher
     * @return
     */
    public LivePushManager setTxLivePusher(TXLivePusher txLivePusher) {
        this.txLivePusher = txLivePusher;
        return this;
    }

    /**
     * 设置 videoView
     *
     * @param mVideoView
     * @return
     */
    public LivePushManager setVideoView(TXCloudVideoView mVideoView) {
        this.mVideoView = mVideoView;
        return this;
    }

    /**
     * 获取new VideoView.
     *
     * @return
     */
    public TXCloudVideoView getlocalVideoView() {
        checkNull();
        if (mVideoView == null) {
            mVideoView = new TXCloudVideoView(context);
        }
        return mVideoView;
    }

    /**
     * 获取当前的 推流对象。
     *
     * @return
     */
    public TXLivePusher getLocTxLivePusher() {
        checkNull();
        if (txLivePusher == null) {
            txLivePusher = new TXLivePusher(context);
        }
        return txLivePusher;
    }

    //-----------提供获取新的相关对象-----------


    public TXLivePushConfig getNewTxLivePushConfig() {
        return new TXLivePushConfig();
    }

    /**
     * 获取new VideoView.
     *
     * @return
     */
    public TXCloudVideoView getNewVideoView() {
        TXCloudVideoView txCloudVideoView = new TXCloudVideoView(context);
        return txCloudVideoView;
    }


    /**
     * 获取新的推流对象
     *
     * @return
     */
    public TXLivePusher getNewTxLivePusher(Context context) {
        TXLivePusher mLivePusher = new TXLivePusher(context);
        return mLivePusher;
    }

    /**
     * 传递
     *
     * @param context
     * @return
     */
    public TXCloudVideoView getNewVideoView(Context context) {
        TXCloudVideoView txCloudVideoView = new TXCloudVideoView(context);
        return txCloudVideoView;
    }

    private void checkNull() {
        if (context == null) {
            throw new RuntimeException("context 为空， 请初始化该类init()");
        }
    }

}
